package moonhy.c2dm_client.viewPages_Test;

import java.util.ArrayList;

import moonhy.c2dm_client.MemberListViewPage;
import moonhy.c2dm_client.R;
import moonhy.c2dm_client.R.id;
import moonhy.c2dm_client.R.layout;
import moonhy.c2dm_client.R.string;
import moonhy.c2dm_client.dw.MemberData;
import moonhy.c2dm_client.dw.MemberData;
import moonhy.c2dm_client.util.httpConnectionManager;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


public class CopyOfMemberListActivity extends Activity{
	ArrayList<MemberData> md_array = new ArrayList<MemberData>();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.member_list_main);
		new initPage(this).execute();
	}
	
	private void setData(Context context){
		//String readNotice = readJSON("http://moonhy.mireene.com/kline/notice/notice_list.php");
		try {
			httpConnectionManager hcm = new httpConnectionManager(context);
			
			JSONObject json = new JSONObject(hcm.readJSON(getString(R.string.SERVER_URL) + "members/member_list.php",null));
			
			JSONObject json_header = json.getJSONObject("header");
			JSONArray json_result = json.getJSONArray("data_result");
			
			for (int i = 0; i < json_result.length(); i++) {
				JSONObject jsonObject = json_result.getJSONObject(i);
				MemberData tmp_nd = new MemberData();
				tmp_nd.setMemberName(jsonObject.getString("mem_name"));
				tmp_nd.setMemberPhone(jsonObject.getString("mem_phone"));
				tmp_nd.setMemberEmail(jsonObject.getString("mem_email"));
				tmp_nd.setMemberBirth(jsonObject.getString("mem_birth"));
				md_array.add(i, tmp_nd);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	private class initPage extends AsyncTask<Void, Void, Void> implements DialogInterface.OnCancelListener{
		private ProgressDialog dialog;
		private Context context;
		
		private initPage(Context context){
			this.context = context;
		}

		protected void onPreExecute(){
			dialog = ProgressDialog.show(CopyOfMemberListActivity.this, "", "Loading. Please wait...", true);
		}

		protected Void doInBackground(Void... unused){
			setData(context);
    		return null;
        }

        protected void onPostExecute(Void unused)
        {
        	/*
            dialog.dismiss();
            ViewPager viewPager = (ViewPager) findViewById(R.id.viewMemberPager);
    		MyPagerAdapter adapter = new MyPagerAdapter(CopyOfMemberListActivity.this, md_array);
    		viewPager.setAdapter(adapter);
    		
    		Display display = getWindowManager().getDefaultDisplay();
    	    double width = display.getWidth();
    		    	
    		final TextView tvHeader = (TextView) findViewById(R.id.MemberViewHeader);
    		tvHeader.setText("1 / " + md_array.size());
    		
    		viewPager.setOnPageChangeListener(new OnPageChangeListener() {
    	
    			@Override
    			public void onPageSelected(int page) {
    				tvHeader.setText((page + 1) + " / " + md_array.size());
    			}
    	
    			@Override
    			public void onPageScrolled(int arg0, float arg1, int arg2) {
    				// TODO Auto-generated method stub
    			}
    	
    			@Override
    			public void onPageScrollStateChanged(int arg0) {
    			}
    		});
    		*/
        }

        public void onCancel(DialogInterface dialog)
        {
        	cancel(true);
        	dialog.dismiss();
        }
	}
	
	
	private class MyPagerAdapter extends PagerAdapter {

		private ArrayList<LinearLayout> views;

		public MyPagerAdapter(Context context, ArrayList<MemberData> md) {
			views = new ArrayList<LinearLayout>();
			
			for(int i = 0 ; i < md.size(); i++){
				views.add(new MemberListViewPage(context, md.get(i)));
			}
		}

		@Override
		public void destroyItem(View view, int arg1, Object object) {
			((ViewPager) view).removeView((LinearLayout) object);
		}

		@Override
		public void finishUpdate(View arg0) {

		}

		@Override
		public int getCount() {
			return views.size();
		}

		@Override
		public Object instantiateItem(View view, int position) {
			View myView = views.get(position);
			((ViewPager) view).addView(myView);
			return myView;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {

		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {

		}

	}

}

